package com.xiaoxian.org.controller;

import com.xiaoxian.basic.domain.vo.ShopStatisticsVo;
import com.xiaoxian.basic.util.ExcelUtil;
import com.xiaoxian.org.domain.ShopAuditLog;
import com.xiaoxian.org.service.IShopService;
import com.xiaoxian.org.domain.Shop;
import com.xiaoxian.org.query.ShopQuery;
import com.xiaoxian.basic.util.AjaxResult;
import com.xiaoxian.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){

            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.my();

    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){

            shopService.delete(id);
            return AjaxResult.my();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.queryOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){

        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.queryPage(query);
    }

    /**
     * 店铺入住
     * @param shop
     * @return
     */
    @PostMapping("/setshop")
    public AjaxResult setShop(@RequestBody @Valid Shop shop){
        shopService.setShop(shop);
        return AjaxResult.my();
    }

    /**
     * 审核通过
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        shopService.pass(shopAuditLog);
        return AjaxResult.my();
    }

    /**
     * 审核驳回
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        shopService.reject(shopAuditLog);
        return AjaxResult.my();
    }

    /**
     * excel导出
     * @param resp
     */
    @GetMapping("/export")
    public void export(HttpServletResponse resp){
        List<Shop> shops = shopService.queryAll();
        ExcelUtil.exportExcel(shops,null,"店铺",Shop.class,"店铺详情.xls",resp);
    }
    /**
     * excel导入
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shops.forEach(System.out::println);
    }

    @GetMapping("/state")
    public AjaxResult getState(){

        return shopService.getState();
    }
}
